package com.hxx.common.core.wcontour.util;

/**
 * 颜色工具类
 * @author hxd
 */
public class ColorUtil {

    /**
     * RGB转换成十六进制
     * @param r
     * @param g
     * @param b
     * @return
     */
    public static String  rgb2Hex(int r,int g,int b){
        return String.format("0xFF%02X%02X%02X", r,g,b);
    }

    /**
     * 16进制颜色字符串转换成rgb
     *
     * @param hexStr
     * @return rgb
     */
    public static int[] hex2RGB(String hexStr) {
        if (hexStr != null && !"".equals(hexStr) && hexStr.length() == 7) {
            int[] rgb = new int[3];
            rgb[0] = Integer.valueOf(hexStr.substring(1, 3), 16);
            rgb[1] = Integer.valueOf(hexStr.substring(3, 5), 16);
            rgb[2] = Integer.valueOf(hexStr.substring(5, 7), 16);
            return rgb;
        }
        return null;
    }

    /**
     * rgb转int类型
     *
     * @param rgb
     * @return
     */
    public static int rgbToInt(int[] rgb) {
        int r = (rgb[0] << 16) & 0x00ff0000;
        int g = (rgb[1] << 8) & 0x0000FF00;
        int b = rgb[2] & 0x000000FF;
        int i = 0xFF000000 | r | g | b;
        return i;
    }

    /**
     * pixel
     * 十六进制转int类型
     *
     * @param hexStr
     * @return
     */
    public static int hexToInt(String hexStr) {
        int[] rgb = hex2RGB(hexStr);
        int r = (rgb[0] << 16) & 0x00ff0000;
        int g = (rgb[1] << 8) & 0x0000FF00;
        int b = rgb[2] & 0x000000FF;
        int i = 0xFF000000 | r | g | b;
        return i;
    }
}
